package com.lx;

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

/**
 * 请求头信息
 */
@Data
@ToString
public class RequestHader implements Serializable {
    public static final ThreadLocal<RequestHader> REQUEST_HADER_THREAD_LOCAL = new ThreadLocal<>();
    /**
     * 版本号
     */
    private String version = "1.0.0";
    /***
     * 请求时间
     */
    private long timestamp;
    /***
     * 用户信息
     */
    private String userId;
    /***
     * 流水号
     */
    private String seqNo;

    RequestHader() {
        this.timestamp = System.currentTimeMillis();
    }

    /***
     *获取头信息
     * @return
     */
    public static RequestHader getRequestHader() {
        RequestHader requestHader = REQUEST_HADER_THREAD_LOCAL.get();
        if (requestHader == null) {
            synchronized (RequestHader.class) {
                requestHader = new RequestHader();
                RequestHader.saveRequestHader(requestHader);
            }
        }
        return requestHader;
    }

    /**
     * 保存头信息
     *
     * @param requestHader
     */
    public static void saveRequestHader(RequestHader requestHader) {
        if (requestHader != null) {
            REQUEST_HADER_THREAD_LOCAL.set(requestHader);
        }
    }

    /***
     * 删除头信息
     */
    public static void delRequestHader() {
        REQUEST_HADER_THREAD_LOCAL.remove();
    }
}
